package com.gitee.hljdrl.tinylog;

import android.text.TextUtils;
import android.util.Log;

import com.ffsky.logger.LogOut;

import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Level;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.writers.FileWriter;
import org.pmw.tinylog.writers.LogcatWriter;

import java.io.File;

/**
 * https://tinylog.org/download
 */
public class TinyLogFileLogout extends com.gitee.hljdrl.logger.LogOut {

    private String mLogcatFormatPattern = "{message}";
    //    private String mInfoFormatPattern = "{date:yyyy-MM-dd HH:mm:ss} [{thread}] {class}.{method}(){level}: {message}";
    private String mFileFormatPattern = "{date:yyyy-MM-dd HH:mm:ss}:{message}";
    //    private String mInfoFormatPattern = "{date:yyyy-MM-dd HH:mm:ss}:{message}";
    //    private String mErrorFormatPattern = "{date:yyyy-MM-dd HH:mm:ss} [{thread}] {class}.{method}(){level}: {message}";
    private String fileLog;
    private boolean mStarted;

    private String mDir;

    public TinyLogFileLogout(String dir) {
        mDir = dir;
        fileLog = new File(dir, buildFileName()).toString();
        Configurator.currentConfig()
                .writer(new LogcatWriter("I"), mLogcatFormatPattern)
                .addWriter(new FileWriter(fileLog, false, true), Level.INFO, mFileFormatPattern)
                .activate();
        mStarted = true;
    }

    public TinyLogFileLogout(String dir, String fileName) {
        mDir = dir;
        String newFileName = fileName;
        if (TextUtils.isEmpty(newFileName)) {
            newFileName = buildFileName();
        }
        fileLog = new File(dir, newFileName).toString();
        Configurator.currentConfig()
                .writer(new LogcatWriter("I"), mLogcatFormatPattern)
                .addWriter(new FileWriter(fileLog, false, true), Level.INFO, mFileFormatPattern)
                .activate();
        mStarted = true;
    }


    public TinyLogFileLogout(String dir, String fileFormatPattern, String logcatFormatPattern) {
        if (fileFormatPattern != null) {
            mFileFormatPattern = fileFormatPattern;
        }
        if (logcatFormatPattern != null) {
            mLogcatFormatPattern = logcatFormatPattern;
        }
        mDir = dir;
        fileLog = new File(dir, buildFileName()).toString();
        Configurator.currentConfig()
                .writer(new LogcatWriter("I"), mLogcatFormatPattern)
                .addWriter(new FileWriter(fileLog, false, true), Level.INFO, mFileFormatPattern)
                .activate();
        mStarted = true;
    }

    private String buildFileName() {
        return toString("app_logger_", System.currentTimeMillis(), ".log");
    }

    @Override
    public void close() {
        if (mStarted) {
            if (Configurator.currentConfig() != null) {
                if (Configurator.currentConfig().activate()) {
                    Configurator.shutdownWritingThread(false);
                }
            }
            mStarted = false;
        }
    }

    @Override
    public void open() {
        if (!mStarted) {
            fileLog = new File(mDir, buildFileName()).toString();
            Configurator.currentConfig()
                    .writer(new LogcatWriter("I"), mLogcatFormatPattern)
                    .addWriter(new FileWriter(fileLog, false, true), Level.INFO, mFileFormatPattern)
                    .activate();
            mStarted = true;
        }
    }

    @Override
    public void open(String fileName) {
        if (!mStarted) {
            String newFileName = fileName;
            if (TextUtils.isEmpty(newFileName)) {
                newFileName = buildFileName();
            }
            fileLog = new File(mDir, newFileName).toString();
            Configurator.currentConfig()
                    .writer(new LogcatWriter("I"), mLogcatFormatPattern)
                    .addWriter(new FileWriter(fileLog, false, true), Level.INFO, mFileFormatPattern)
                    .activate();
            mStarted = true;
        }
    }

    @Override
    public void setOption(String key, Object option) {
        if (TextUtils.isEmpty(key)) {
            return;
        }
        if (TextUtils.equals(key, OPTION_SWITCH)) {
            if (option instanceof Boolean) {
                mStarted = (boolean) option;
            }
        }
    }

    @Override
    public String getLoggerFile() {
        return fileLog;
    }

    @Override
    public void i(String tag, String msg) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", msg));
        }
    }

    @Override
    public void i(String tag, String... msg) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", toString(msg)));
        }
    }

    @Override
    public void i(String tag, Object... msg) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", toString(msg)));
        }
    }

    @Override
    public void d(String tag, String msg) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", msg));
        }
    }

    @Override
    public void d(String tag, String... msg) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", toString(msg)));
        }
    }

    @Override
    public void d(String tag, Object... msg) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", toString(msg)));
        }
    }

    @Override
    public void e(String tag, String msg) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", msg));
        }
    }

    @Override
    public void e(String tag, Object... msg) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", toString(msg)));
        }
    }

    @Override
    public void e(String tag, Throwable throwable) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", Log.getStackTraceString(throwable)));
        }
    }

    @Override
    public void e(String tag, String msg, Throwable throwable) {
        if (mStarted) {
            Logger.info(toString("[", tag, "]", msg, Log.getStackTraceString(throwable)));
        }
    }
}
